package com.tbsoft.ehr.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tbsoft.ehr.util.RandomValidateCode;
/**
 * @desc 生成验证码
 * @author vidy
 *
 */
public class VeriCodeImageServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("image/jpeg");
		resp.setHeader("Pragma", "no-cache");
		resp.setHeader("Cache-Control", "no-cache");
		resp.setDateHeader("Expire", 0);
		RandomValidateCode rvc = new RandomValidateCode();
		BufferedImage bufImg = rvc.getVeriImg();
		String veriCode = rvc.getVeriCode();
		HttpSession session=req.getSession();
		session.removeAttribute("randCode");
		session.setAttribute("randCode", veriCode);
		ImageIO.write(bufImg, "jpeg", resp.getOutputStream());
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
